package com.pxst.tx.demo;

import java.io.UnsupportedEncodingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.codingapi.txlcn.tc.annotation.DTXPropagation;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.pxst.constants.ResCodeContants;
import com.pxst.resp.ApiResp;
import com.pxst.tx.demo.mapper.TestMapper;

/** 
* @author yangxy
* @version 创建时间：2023年7月28日 上午11:39:32 
*/
@RestController
public class TestController {
	@Autowired
	private TestMapper testMapper;
	@Autowired(required = false)
	private RestTemplate restTemplate;
	
	@GetMapping("/test")
	@LcnTransaction(propagation = DTXPropagation.REQUIRED) //分布式事务发起方
	public void test(String tt) throws UnsupportedEncodingException {
		String url = "http://localhost:8000/test?name="+tt+"_2";

		//1. getForObject()
		//先获取返回的字符串，若想获取属性，可以使用gson转化为实体后get方法获取
		
		
		String result = restTemplate.getForObject(url, String.class);
		ApiResp javaObject = JSONObject.toJavaObject(JSON.parseObject(result), ApiResp.class);
		if(!javaObject.getResCode().equals(ResCodeContants.SUCESS)) {
			throw new RuntimeException("");
		}
		TestEntity t = new TestEntity();
		t.setName(tt);
		testMapper.insert(t);
	}
}
